package com.shuangti.contrllor;

import com.shuangti.pojo.Result;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author: yangL
 * @date: 2025/11/11 16:40:08
 * @version: 1.0
 */
@RestController
public class LoginController {

    /**
     * 生成验证码
     *
     * @param session HttpSession对象
     * @return 验证码图片数据
     */
    @GetMapping("/captcha")
    public Result getCaptcha(HttpSession session) {
        // 生成随机验证码
        //String captcha = generateCaptcha(4);
        String captcha = "1234";
        System.out.println(captcha);
        // 将验证码存储在session中
        session.setAttribute("captcha", captcha);
        
        // 返回验证码信息
        Map<String, String> data = new HashMap<>();
        data.put("captcha", captcha);
        
        return Result.success(data);
    }

    /**
     * 处理登录请求
     *
     * @param userReq 包含用户名、密码和验证码的请求体
     * @param session HttpSession对象
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> userReq, HttpSession session) {
        String username = userReq.get("username");
        String password = userReq.get("password");
        String captcha = userReq.get("captcha");
        
        // 获取session中的验证码
        String sessionCaptcha = (String) session.getAttribute("captcha");
        
        // 检查验证码
        if (captcha == null || sessionCaptcha == null || !captcha.equalsIgnoreCase(sessionCaptcha)) {
            // 验证码错误，重新生成验证码
            session.removeAttribute("captcha");
            return Result.error("验证码错误");
        }
        
        // 验证用户名和密码
        if ("admin".equals(username) && "123".equals(password)) {
            // 登录成功，将用户信息存储在session中
            session.setAttribute("user", username);
            session.removeAttribute("captcha"); // 清除已使用的验证码
            
            // 返回成功信息
            Map<String, Object> data = new HashMap<>();
            data.put("username", username);
            return Result.success(data);
        } else {
            // 登录失败，重新生成验证码
            session.removeAttribute("captcha");
            return Result.error("用户名或密码错误");
        }
    }

    /**
     * 处理登出请求
     *
     * @param session HttpSession对象
     * @return 登出结果
     */
    @PostMapping("/logout")
    public Result logout(HttpSession session) {
        session.invalidate();
        return Result.success();
    }

    /**
     * 检查登录状态
     *
     * @param session HttpSession对象
     * @return 登录状态信息
     */
    @GetMapping("/api/check-login")
    public Result checkLoginStatus(HttpSession session) {
        String user = (String) session.getAttribute("user");
        if (user != null) {
            Map<String, Object> data = new HashMap<>();
            data.put("username", user);
            return Result.success(data);
        } else {
            return Result.error("用户未登录");
        }
    }

    /**
     * 生成指定长度的随机验证码
     *
     * @param length 验证码长度
     * @return 随机验证码
     */
    private String generateCaptcha(int length) {
        String chars = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";
        StringBuilder captcha = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            captcha.append(chars.charAt(random.nextInt(chars.length())));
        }
        return captcha.toString();
    }
}